package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tiantian.bean.PageBean;
import com.tiantian.dao.BusinessrechargedetailsRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Businessrechargedetails;
import com.tiantian.domain.BusinessrechargedetailsId;
import com.tiantian.service.BusinessrechargedetailsService;
import com.tiantian.util.CommonConstant.QueryType;
import com.tiantian.util.ConditionResult;

@Service
@Transactional
public class BusinessrechargedetailsServiceImpl extends GenericServiceImpl<Businessrechargedetails, BusinessrechargedetailsId> implements
		BusinessrechargedetailsService {
	private BusinessrechargedetailsRepository repository;

	@Autowired
	public void setRepository(BusinessrechargedetailsRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	/**
	 * 获取商家充值详情信息
	 * 
	 * @param params
	 * @param pageBean
	 * @return
	 */
	@Override
	public List<Map<String, Object>> rechargeDetailsList(Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getRechargeDetails(params, QueryType.LIST);
		List<Map<String, Object>> ret = repository.queryHumpListMapBySql(conditionResult.getSql(), pageBean.getStartIndex(), pageBean.getPageSize(),
				conditionResult.getParams());
		conditionResult = getRechargeDetails(params, QueryType.COUNT);
		pageBean.setTotal(repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams()));
		return ret;
	}

	private ConditionResult getRechargeDetails(Map<String, String> searchMap, QueryType type) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		switch (type) {
		case LIST:
			sql.append(" select t.*,bank.bankname ");

			break;
		case COUNT:
			sql.append(" select count(1)");
			break;
		default:
			break;
		}
		sql.append(" from BusinessRechargeDetails t left join banklist bank on bank.bankid=t.AccountBank where 1=1 ");

		if (searchMap != null) {

			if (StringUtils.isNotBlank(searchMap.get("statusInfor"))) {
				sql.append(" and t.statusInfor = :statusInfor ");
				params.put("statusInfor", searchMap.get("statusInfor").trim());
			}
			if (StringUtils.isNotBlank(searchMap.get("businessUserID"))) {
				sql.append(" and t.businessUserID like :businessUserID ");
				params.put("businessUserID", "%" + searchMap.get("businessUserID").trim() + "%");
			}
			if (StringUtils.isNotBlank(searchMap.get("minRechargeAmount"))) {
				sql.append(" and t.RechargeAmount >= :minRechargeAmount ");
				params.put("minRechargeAmount", searchMap.get("minRechargeAmount"));
			}
			if (StringUtils.isNotBlank(searchMap.get("maxRechargeAmount"))) {
				sql.append(" and t.RechargeAmount <= :maxRechargeAmount ");
				params.put("maxRechargeAmount", searchMap.get("maxRechargeAmount"));
			}
			if (StringUtils.isNotBlank(searchMap.get("startApplicationRechargeTime"))) {
				sql.append(" and t.ApplicationRechargeTime >= :startApplicationRechargeTime ");
				params.put("startApplicationRechargeTime", searchMap.get("startApplicationRechargeTime"));
			}
			if (StringUtils.isNotBlank(searchMap.get("endApplicationRechargeTime"))) {
				sql.append(" and t.ApplicationRechargeTime <= :endApplicationRechargeTime ");
				params.put("endApplicationRechargeTime", searchMap.get("endApplicationRechargeTime"));
			}
		}
		if (type == QueryType.LIST) {
			sql.append(" order by t.ApplicationRechargeTime desc");
		}
		return new ConditionResult(sql.toString(), params);

	}

	@Override
	public List<Businessrechargedetails> getRechargeDetails(Map<String, String> param) {
		String[] ids = StringUtils.split(param.get("businessUserID"), ",");
		String[] times = StringUtils.split(param.get("applicationRechargeTime"), ",");
		Map<String, Object> params = new HashMap<>();
		String temp = "select * from  Businessrechargedetails  where ";
		for (int i = 0; i < ids.length; i++) {

			String id = ids[i];
			String time = times[i];
			temp += " ( businessUserID=:id_" + i;
			temp += " and UNIX_TIMESTAMP(applicationRechargeTime)*1000=:time_" + i + " )";
			if (i != ids.length - 1) {
				temp += " or ";
			}
			params.put("id_" + i, id);
			params.put("time_" + i, time);
		}
		return repository.queryListBySql(temp, params, Businessrechargedetails.class);

	}

	@Override
	public Long count(Map<String, String> params) {
		ConditionResult conditionResult = getRechargeDetails(params, QueryType.COUNT);
		return repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams());
	}

}
